# -*- coding: utf-8 -*-
"""
Created on Tue Jan 12 10:24:39 2016

@author: wuyf
"""

import xdrlib,sys
import xlrd
from xlwt import*

style_red = easyxf('pattern: pattern solid')
style_red.font.colour_index = 2
style_red.pattern.pattern_fore_colour = 1
borders = Borders()
borders.left = Borders.DASHED # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
borders.right = Borders.DASHED
borders.top = Borders.DASHED
borders.bottom = Borders.DASHED
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style_red.borders = borders

def open_excel(file='file.xls'):
    try:
        data = xlrd.open_workbook(file)
        return data
    except Exception,e:
        print str(e)

def split2person(table,worker_name=u'武永峰'):    
    
    #行数
    nrows = table.nrows
    #列数
    ncols = table.ncols
    ignore_time = u'晚上'
    w =Workbook()

    ws = w.add_sheet('sheet1')
    #ws.write(0,0,u'武永峰')
    cur_row = 0
    #编写表头
    ws.write(cur_row,0,u'日期')
    ws.write(cur_row,1,u'工号')
    ws.write(cur_row,2,u'姓名')
    ws.write(cur_row,3,u'部门')
    ws.write(cur_row,4,u'班次')
    ws.write(cur_row,5,u'上班时间&下班时间')
    ws.write(cur_row,6,u'签到时间&签退时间')
    ws.write(cur_row,7,u'异常')
    ws.write(cur_row,8,u'工作时长') 
    ws.write(cur_row,9,u'加班')
    ws.write(cur_row,10,u'请假')
    ws.write(cur_row,11,u'其他')

    cur_row +=1

    for i in range(nrows):
        if table.cell(i,2).value == worker_name and not table.cell(i,4).value ==ignore_time:
            for j in range(ncols):
                value = table.cell(i,j).value
                #如果是异常，则标红
                if not table.cell(i,7).value == u'正常':       
                    ws.write(cur_row,j,value,style_red)
                else:    
                    ws.write(cur_row,j,value)
            cur_row += 1            
    #添加统计信息
    cur_row +=2
    ws.write(cur_row,0,u'迟到')
    ws.write(cur_row,2,u'早退')
    ws.write(cur_row,4,u'缺勤')
    ws.write(cur_row,6,u'请假')
    cur_row +=1
    #添加一行注释
    ws.write_merge(cur_row,cur_row,0,5,u'注：如果有缺勤或请假，填写明缺勤或请假的天数',style_red)
    cur_row += 1

    #保存
    w.save(worker_name+'.xls')

data = open_excel(u'异常统计.xls')
table = data.sheet_by_index(0)
walkers = [u'武永峰',u'周智勇',u'郑潇平',u'吴怡洁',u'周汝勤',u'丁磊',u'王鹏源']
for walker_name in walkers:
    split2person(table,walker_name)
